// Phantomjs、Selenium, Nightmare(基于electronjs), Puppeteer
// 文档 https://zhaoqize.github.io/puppeteer-api-zh_CN/#?product=Puppeteer&version=v10.0.0&show=api-class-puppeteer
const fs = require('fs');
const puppeteer = require('puppeteer');
const util = require('./util');

let pid = 0;

// 打开无头浏览器
// 设置浏览器的信息为手机
// 修改近地铁规则，触发页面信息
// 监听 response 获取房源数据

async function autoScroll(page){
    await page.evaluate(async () => {
        await new Promise((resolve, reject) => {
            var totalHeight = 0;
            var distance = 100;
            var timer = setInterval(() => {
                var scrollHeight = document.body.scrollHeight;
                window.scrollBy(0, distance);
                totalHeight += distance;

                if(totalHeight >= scrollHeight){
                    clearInterval(timer);
                    resolve();
                }
            }, 100);
        });
    });
}

(async () => {
    // 打开无头浏览器
    // 设置为false显示无头浏览器，用来关注数据
    let browser = await puppeteer.launch({ headless: false });
    // 打开新页面
    let page = await browser.newPage();
    // 去除掉navigator.webdriver属性，防止被反爬识别
    /* 
    * 目前大多数方案是 ignoreDefaultArgs: [‘--enable-automation‘]
    * Object.defineProperty(navigator, ‘webdriver‘, {
        get: () => undefined,
    })
    实测均存在缺陷。 方案1无法成功， 方案二无法阻止使用 ‘webdriver‘ in navigator识别
    */
    await page.evaluateOnNewDocument(() => {
        const newProto = navigator.__proto__;
        delete newProto.webdriver;
        navigator.__proto__ = newProto;
    });
    // 设置浏览器内核为手机浏览器
    await page.emulate(puppeteer.devices['iPhone X']);
    // 打开贝壳二手房
    await page.goto('https://m.ke.com/bj/ershoufang/p1p2p3lc1lc2lc3lc4sf1');
    await util.sleep(200);
    // 贝壳会默认让你跳转app， 点击仍然使用浏览器
    await page.waitForSelector('.bannerWrapper:nth-child(2)');
    let bannerWrapper = await page.$('.bannerWrapper:nth-child(2)');
    if (bannerWrapper) {
        await Promise.all([
            page.waitForNavigation(),
            page.click('.bannerWrapper:nth-child(2)')
        ]);
    }
    await page.waitForSelector('.conch-ui-tab-item:nth-child(2)');
    await util.sleep(300);
    // 不知道为何使用 page.click和page.tap无法触发点击事件。
    let n = await page.$eval('.conch-ui-tab-item:nth-child(2)', el => el.click());
    // response的时候把自己关注的返回体写入到文件中
    // https://m.ke.com/liverpool/api/ershoufang/getList?cityId=110000&condition=%252Fp1p2p3lc1lc2lc3lc4sf1
    // ajax
    let content = await page.content();

    

    page.on('response', async (r) => {
        if (/\/api\/ershoufang\/getList/.test(r.url())) {
            r.json().then(async result => {
                if (result.data.data.getErShouFangList.returnCount === 30) {
                    fs.appendFileSync('./house.js', `page.page${pid} = ${JSON.stringify(result.data.data.getErShouFangList.list)}; \n`, 'utf8');
                    pid = pid + 1;
                    await util.sleep(Math.round(Math.random() * 1000));
                    await autoScroll(page);
                }
            })
        }
    })
})();